package com.example.zhihuigongdi.service.impl;

import com.example.zhihuigongdi.commer.CommonException;
import com.example.zhihuigongdi.entity.Cameras;
import com.example.zhihuigongdi.entity.Recordings;
import com.example.zhihuigongdi.entity.User;
import com.example.zhihuigongdi.entity.respon.Result;
import com.example.zhihuigongdi.mapper.CamerasMapper;
import com.example.zhihuigongdi.service.ICamerasService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhihuigongdi.service.IPersonnelService;
import com.example.zhihuigongdi.utils.CheckUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhongbin
 * @since 2024-04-17
 */
@Service
public class CamerasServiceImpl extends ServiceImpl<CamerasMapper, Cameras> implements ICamerasService {

    @Autowired
    private CheckUtil checkUtil;

    @Lazy
    @Autowired
    private ICamerasService camerasService;
    @Override
    public Result addCamera(Cameras cameras) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);
        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        camerasService.save(cameras);

        return Result.success("添加成功");

    }

    @Override
    public Result removeCamera(int camera_id) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);
        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        camerasService.removeById(camera_id);
        return Result.success("删除成功");
    }

    @Override
    public Result getCameras(int current, int size) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        PageHelper.startPage(current,size);
        List<Cameras> list = camerasService.list();
        PageInfo<Cameras> camerasPageInfo = new PageInfo<>(list);
        return Result.success(camerasPageInfo);
    }
}
